import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zootix/constants/zt_constants.dart';
import 'package:zootix/models/zt_followed.model.dart';
import 'package:zootix/screens/follow_conversation/zt_followed.controller.dart';
import 'package:zootix/store/zt_auth.store.dart';
import 'package:zootix/util/zt_screen_adaper_util.dart';
import 'package:zootix/widgets/zt_empty.dart';
import 'package:zootix/widgets/zt_fade_in_cache_image.dart';

class ZtFollowedList extends StatelessWidget {
  final _controller = Get.put<ZtFollowedController>(ZtFollowedController());
  ZtFollowedList({super.key});

  @override
  Widget build(BuildContext context) {
    return _buildList();
  }

  Widget _buildList() {
    return Obx(() => SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        controller: _controller.refreshController,
        onRefresh: _controller.onRefresh,
        onLoading: _controller.onLoading,
        child: _controller.followedList.isEmpty &&
                _controller.refreshController.headerStatus ==
                    RefreshStatus.completed
            ? ZtEmpty(text: "No followed.")
            : ListView.builder(
                itemCount: _controller.followedList.length,
                itemBuilder: (_, index) => _buildFollowedItem(
                    _controller.followedList[index], index))));
  }

  Widget _buildFollowedItem(ZtFollowedModel followedModel, int index) {
    return Container(
      padding: EdgeInsets.symmetric(
          vertical: ZtScreenAdaper.height(20),
          horizontal: ZtScreenAdaper.width(20)),
      child: Row(children: [
        InkWell(
            onTap: () {
              Get.toNamed(ZtRouteConfig.privateChat, arguments: {
                'nickname': followedModel.nickname,
                'userId': followedModel.userId,
                'avatar': followedModel.avatarUrl
              })?.then((value) => {
                    if (value == ZtBackActionEnum.refresh)
                      {_controller.refreshController.requestRefresh()}
                  });
            },
            child: Container(
              clipBehavior: Clip.hardEdge,
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
              ),
              width: ZtScreenAdaper.width(50),
              child: ZtFadeInCacheImage(
                width: ZtScreenAdaper.width(50),
                url: followedModel.avatarUrl ?? '',
              ),
            )),
        SizedBox(
          width: ZtScreenAdaper.width(15),
        ),
        Expanded(
            child: Text(
          followedModel.nickname!,
          maxLines: 2,
          style: TextStyle(
              overflow: TextOverflow.ellipsis,
              fontSize: ZtScreenAdaper.sp(16),
              fontWeight: FontWeight.w500),
        )),
        SizedBox(
          width: ZtScreenAdaper.width(10),
        ),
        InkWell(
          onTap: () {
            _controller.unFollowed(followedModel.userId!, index);
          },
          child: Container(
            padding: EdgeInsets.symmetric(
                horizontal: ZtScreenAdaper.width(10),
                vertical: ZtScreenAdaper.height(10)),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(ZtScreenAdaper.sp(20)),
                gradient: const LinearGradient(
                    colors: [Color(0xFFB0F320), Color(0xFFABEE1C)])),
            child: Text(
              'Followed',
              style: TextStyle(
                  fontSize: ZtScreenAdaper.sp(16), color: Colors.white),
            ),
          ),
        )
      ]),
    );
  }
}
